//
//  NWStatusCell.swift
//  Weibo
//
//  Created by Northwan on 16/1/24.
//  Copyright © 2016年 Northwan. All rights reserved.
//

import UIKit
import SnapKit
/// 默认间距
let NWStatusCellSubViewsMargin:CGFloat = 10

class NWStatusCell: UITableViewCell {
    
    var toolBarTopConstraint:Constraint?
    
    var statusViewModel:NWStatusViewModel?{
        didSet{
            //填充数据
            statusOriginalView.statusViewModel = statusViewModel
            statusToolBar.statusViewModel = statusViewModel
            
            //卸载约束
            toolBarTopConstraint?.uninstall()
            
            if statusViewModel?.status?.retweeted_status != nil{
                statusRetweetView.hidden = false
                //有转发微博
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                   toolBarTopConstraint =   make.top.equalTo(statusRetweetView.snp_bottom).constraint
                })
                statusRetweetView.statusViewModel = statusViewModel
            }else{
                statusRetweetView.hidden = true
                //没有转发微博
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                  toolBarTopConstraint =  make.top.equalTo(statusOriginalView.snp_bottom).constraint
                })
            }
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        //添加view
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusRetweetView)
        contentView.addSubview(statusToolBar)
        
        //添加约束
        statusOriginalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        statusRetweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(statusOriginalView.snp_bottom)
            make.leading.equalTo(statusOriginalView)
            make.trailing.equalTo(statusOriginalView)
        }
        
        statusToolBar.snp_makeConstraints { (make) -> Void in
            toolBarTopConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        //约束contentView,(重要)
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(statusToolBar.snp_bottom)
        }
    }
    
    // MARK: - 懒加载
    
    private lazy var statusOriginalView:NWStatusOriginalView = NWStatusOriginalView()
    
    private lazy var statusToolBar:NWStatusToolBar = NWStatusToolBar()
    
    private lazy var statusRetweetView:NWStatusRetweetView = NWStatusRetweetView()
}
